package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;

import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "9021000139623599";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCo+1XtUEb4bmY/X+SUmvhWMdDb21yrcbVz8KiEVGCqQIownDB0ULO2rYJ3PMdbJA3DZRgtnkjYeG8oSBYSZ+bZKQ7ZSZHNhecyvtTVIUbOZtYSAIbBz0OuWMNKMwfpo7OsVcHWyY8/u1hRu7NV/36ZddeHiqCpXQ5Yqyavspk3eb3L9vN0mH3sd1qhYqxfPh/CWVnWqpmxOH3HJiPTUlPJ4PoFneQ+ZcELPSHfZTUahf8JLfg+M1A2CCPy03UtPv9lBIP31AQ/Snu0rlcLRxwhv/hKNGypGdGw0iCA1BwWgV9JGZRVQ6KxRV7fUIcrUi5rYGPx90BohI+AaJOTfxXXAgMBAAECggEAZdRXmpOmwOyeVxBtSZP9lMueRe7x8CqsovpiQqQqQE52OAbXODCtq6bxmPMaW3TlilrwcAUTdsdUjNyr2Fmehjy0wwIPh99qTG0tCmZsKdNUqzKAAmjpKzY4MQ2Bdiz9CBwBnHKIDWrTNSW8U1WMr+OQhxBl8smt7Ibu4C/rHr8RfA5/OvsrbsfuUKdKvpA9AS/SHQxjJngEYlhy1UjXpwM1t9iV+8Cr3zpqg0wmTOwQuf+03q3ay1lAie1am8osuTeN0kIl7hqle3j8tO5YMR1TbbNxBEn89H6eWYPa1jvzirE7gVLvIIYb8zV+FpaesvG5RJhdmv6a19+jL0SY0QKBgQD+TK85SCfyFiNWP96XWMc09v94s1UU7px4exTxvE1sJX1Y7jVR31mDNlb3pq9yU/C8U8esOVMrhHMusacw1cQ8esJIuHLG1CRdcPX5KoBhJj14wXEi0Q+2UYvhA+u6ByMZzs70BYMwsK5WqVV+qP7lS8uT+GgCRVLVUO4bcIkDzwKBgQCqHJo0U6rF1wT5F3kHB/zpqD6tqFTwvbA7p3kkd1VNsr+Ar88BBQNJHiOfvQ1Pv9HdVWpZYfyzXwf/Z4xKTEKkWWFvsiqJJePGpp84o3e5mvK4Zu1DJKH2gu9/jN2RipHQAPkMA/jw3+sYnXp4eZUxw7T2aXhOur7t4/vpQ4xneQKBgQChefI+LmpnF9Wie/YNJquAH2ODsmRRYV7sRkO7aHXFOV0J7mDfNzMw1X2NzkNUFqcj/xMoclI0rOsy+E4Q/XyZZt4drTn8tVLOHt+iM3HHvq+EEVmU4SnX/JoU7RdvTf7k8WSKoUF31oktsGMAhbEtyt5ffHoXDwwVNkmsFN58EwKBgQCmSHs79yURatODobw+cUJ6LGnIWqldAi8IwPYb/UI3RykG49PXTU0e5gUwmh4u/HRqhJoGcVARDi3FLTTnU5A5Et25z1Dg61ax0iIFvVTE63+jXYlDfsWLMAZoigvzvp410x2e7ysiWd93GkijB0YddQ6paAOgAc+svpR6Ofs/EQKBgDbBqxNtpep4/1KIJvZLmwAExfPLStvgHBrzanAKMlLxSM14SwJJ1XdP1736T3/D1Q78GR8somiy3/dotUz4sta+B11BHrokpzeu+i+JQ4sqZQdwRQ5xV6uSvJwdYJIVcOpO1X+UbAOYm0tGyLHv3dxzIwuccaV2EZhU3Fn7uDQA";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxy3ajxOrCSEDXwc+AFWo85ZSlnoSQmvbSo76jC51vVQkZ3osZ7xVvch4I0bnvuL5Dz6jpUjb6YMIgO6A1VmcNoFiAXASVvCUYQQ6+VVzqxMdq+mghChhr3p657mnAx8xplZEDbTDzjL7qZTcdYFGiPsDbO+esX71Dnc3NvoOCis60BxkevME3uEr3CdUZdauZdyaVxTC1CxkmneuSUVJAVHacI3jpBqhvlap0rfSQzAjQZb2ULCmuTdJ6eHKIRruF+dLfl2JZQMhryFIJ+r9hQPYE/A6+t/KQ6jPNUeqySPeJHOAAsTtSx1NQ9Nk4MstSp8yYI9chXcvDIP4EPm7GwIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url ="http://bphtnp.natappfree.cc/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
//    private  String return_url ="http://42wf8p.natappfree.cc/return_url.jsp";
    private  String return_url ="http://member.mall.com/memberOrder.html";
    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";
    //超时时间
    private String timeOut = "30m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+ timeOut +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
